package com.desktop.click.tool.frame;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.desktop.click.tool.entity.RobotClickProperties;
import com.desktop.click.tool.util.MouseUtil;
import com.desktop.click.tool.util.SerializableUtil;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.Vector;

public class Add extends JFrame {
    {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    private Vector<RobotClickProperties> data;

    private Main main;

    private JTextField field1;
    private JTextField field2;
    private JTextField field3;

    private JLabel label1;
    private JLabel label2;
    private JLabel label3;



    private JButton jButton1;
    private JButton jButton2;

    private Add add;

    public Add(Vector<RobotClickProperties> data, Main main) throws IOException {
        setIconImage(ImageIO.read(new File("ico/main.jpeg")));
        this.main = main;
        this.data = data;
        add = this;
//        setUndecorated(true);
        setTitle("添加辅助参数");
        setLayout(null);
        setBounds(800, 240, 400, 300);
        field1 = new JTextField();
        field1.setBounds(155, 20, 100, 30);
        field1.setEditable(false);
        label1 = new JLabel();
        label1.setBounds(100, 20, 100, 30);
        label1.setText("横坐标：");
        add(field1);
        add(label1);


        field2 = new JTextField();
        field2.setBounds(155, 65, 100, 30);
        field2.setEditable(false);
        label2 = new JLabel();
        label2.setBounds(100, 65, 100, 30);
        label2.setText("纵坐标：");
        add(field2);
        add(label2);


        field3 = new JTextField();
        field3.setBounds(155, 105, 100, 30);
        label3 = new JLabel();
        label3.setBounds(100, 105, 100, 30);
        label3.setText("间隔时间：");
        add(field3);
        add(label3);

        jButton1 = new JButton();

        jButton1.setText("保存");
        jButton1.setBounds(10, 200, 80, 30);
        jButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (checkAndSubmitForm()) {
                    System.out.println(data);
                    main.reloadTable();
                    clearInput();
                    SerializableUtil.saveLocalFile(data);
                    add.setVisible(false);
                }
            }
        });


        jButton2 = new JButton();
        jButton2.setText("取消");
        jButton2.setBounds(290, 200, 80, 30);
        jButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                clearInput();
                add.setVisible(false);
            }
        });
        add(jButton1);
        add(jButton2);
        setVisible(true);

    }


    public void setLocation(){
        field1.setText(MouseUtil.getMouseX().toString());
        field2.setText(MouseUtil.getMouseY().toString());
    }
    public Integer getInputVal(JTextField field) {
        String text = field.getText();
        return Integer.parseInt(text);
    }

    public void clearInput() {
        field1.setText("");
        field2.setText("");
        field3.setText("");
    }

    public boolean checkAndSubmitForm() {
        if (!NumberUtil.isInteger(field1.getText())) {
            JOptionPane.showMessageDialog(null, "请输入正整数的横坐标", "消息提示", JOptionPane.WARNING_MESSAGE);
            return false;
        }
        if (!NumberUtil.isInteger(field2.getText())) {
            JOptionPane.showMessageDialog(null, "请输入正整数的纵坐标", "消息提示", JOptionPane.WARNING_MESSAGE);
            return false;
        }
        if (!NumberUtil.isInteger(field3.getText())) {
            JOptionPane.showMessageDialog(null, "请输入正整数的间隔时间(毫秒)", "消息提示", JOptionPane.WARNING_MESSAGE);
            return false;
        }
        Integer inputVal1 = getInputVal(field1);
        Integer inputVal2 = getInputVal(field2);
        Integer inputVal3 = getInputVal(field3);
        if (inputVal3 < 100) {
            JOptionPane.showMessageDialog(null, "间隔时间不能小于100ms", "消息提示", JOptionPane.WARNING_MESSAGE);
            return false;
        }

        return data.add(new RobotClickProperties(IdUtil.fastSimpleUUID().substring(0,10), inputVal1, inputVal2, inputVal3));

    }


}
